home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15269 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  92 lines

  1. Path: news.belwue.de!horn!kuehl
  2. From: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: gnu.g++.help,comp.sys.lang.c++,comp.lang.c++
  4. Subject: Re: Help needed on function templates
  5. Followup-To: gnu.g++.help,comp.sys.lang.c++,comp.lang.c++
  6. Date: 4 Apr 1996 15:40:56 GMT
  7. Organization: FakultΣt fⁿr Mathematik und Informatik
  8. Message-ID: <4k0qi8$r4f@news.BelWue.DE>
  9. References: <NEWTNews.828674472.23501.seedy@trigent.trigent.com>
  10. Reply-To: dietmar.kuehl@uni-konstanz.de
  11. NNTP-Posting-Host: horn.informatik.uni-konstanz.de
  12. X-Newsreader: TIN [version 1.2 PL2]
  13.  
  14. Hi,
  15.  
  16. seedy@ultranet.com wrote:
  17. : When attempted to compile the following code I got the following error :
  18. : I use g++ (2.7.1) on HP-UX 10.x.
  19.  
  20. : news.cc: In function `int equal(const class abc &, const class abc &)':
  21. : news.cc:23: no match for `operator ==(class abc, class abc)'
  22.  
  23. There is indeed no 'operator==()' defined in your class which takes a
  24. const object as first argument and there is no built-in conversion from
  25. a const object to a non-const one (sure: this would mean to create a
  26. non-const unnamed temporary but all unnamed temporaries are const).
  27. Thus, all you need to do, is to declare your 'operator==()' as it
  28. should be declared: as 'const'.
  29.  
  30. : #include <iostream.h>
  31.  
  32. : class abc {
  33. :     public : 
  34. :        abc( int x )
  35. :       {
  36. :       a = x;
  37. :       b = 0;
  38. :       }
  39. :        int a;
  40. :        int b;
  41. :        inline int operator ==(abc x)
  42.          inline int operator == (abc x) const
  43.  
  44. Actually, I think this operator should be declared as
  45.      inline bool operator == (abc const &x) const
  46.  
  47. The implementation can stay as is...
  48. :            {
  49. :               return ((a==x.a) && (b==x.b));
  50. :            }
  51. :     };
  52.  
  53.  
  54.  
  55. : template < class Element>
  56. : inline int equal (Element const& e1, Element const& e2) 
  57. : {
  58. :   return e1 == e2;
  59.  
  60. : }
  61.  
  62. : main()
  63. : {
  64. : abc a1(5), a2(5);
  65.  
  66. : cout << "Return value is = " << equal(a1, a2) << endl;
  67. : }
  68.  
  69.  
  70. : Even though the 'operator ==' is overloaded in class abc, compiler is
  71. : reporting match not found. By making the overloaded function 'operator =='
  72. : as friend to 'class abc' I could resolve the problem, because in this case,
  73. : this overloaded function becomes outsider function.
  74.  
  75. You probably declared your "outsider" 'operator==' like this:
  76.   int operator== (abc a, abc b) { /*...*/ }
  77.  
  78. In this case, a (non-const) copy is made of the 'const' arguments
  79. passed to it in the 'equal()' function. This is the reason why it
  80. worked then.
  81.  
  82. : As far as I know, template instantiation for function 'equal will be done
  83. : for 'class abc'(because the passed parameters are of type class abc), 
  84. : and so the corresponding member function 'operator ==' can be used for 
  85. : template variables e1 and e2.
  86.  
  87. You are right: for non-const variables it should work...
  88. --
  89. dietmar.kuehl@uni-konstanz.de
  90. http://www.informatik.uni-konstanz.de/~kuehl/
  91. I am a realistic optimist - that's why I appear to be slightly pessimistic
  92.